package shizhong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class TimerPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Graphics2D g2;
	int hour;
	int minute=30;
	int second;
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(new Color(18, 56, 86, 150));
		g2.setStroke(new BasicStroke(1f));
		g2.drawOval(100, 100, 400, 400);
		g2.setStroke(new BasicStroke(5f));
		g2.setColor(new Color(18, 56, 86, 100));
		g2.drawOval(102, 102, 396, 396);
		g2.setColor(new Color(18, 56, 86, 150));
		g2.setStroke(new BasicStroke(1f));
		g2.drawOval(104, 104, 392, 392);
		g2.setColor(new Color(255, 255, 255, 150));
		g2.fillOval(104, 104, 392, 392);
		g2.setColor(new Color(18, 56, 86, 150));
		g2.setStroke(new BasicStroke(2f));
		
		for (int i = 0; i < 60; i++) {
			int x1, y1;
			if (i % 5 == 0) {
				g2.setStroke(new BasicStroke(6f));
			} else
				g2.setStroke(new BasicStroke(1f));
			x1 = (int) Math.round(300 + Math.cos(i * Math.PI / 30) * 192);
			y1 = (int) Math.round(300 + Math.sin(i * Math.PI / 30) * 192);
			g2.drawLine(x1, y1, 300, 300);			
		}
		g2.setColor(Color.white);
		g2.fillOval(116, 116, 368, 368);
		
		paintZhen(hour, Color.black, 8f, 100,true);//时针
		paintZhen(minute, Color.black, 5f, 150,false);//分针
		paintZhen(second,Color.red,2f,190,false);//秒针
		g2.setColor(Color.red);
		g2.fillOval(295, 295, 10, 10);	
	}

	public void paintZhen(int time, Color color, float weight, int length,boolean isHour) {
		if (g2 != null) {
			double si = 0;
			g2.setColor(color);
			g2.setStroke(new BasicStroke(weight)); 
			si = time - 15 >= 0 ? time - 15 : time + 45;
			if (isHour) si=si*5+minute/12.0;				
			int sx = (int) Math.round(300 + Math.cos(si * Math.PI / 30)	* length);
			int sy = (int) Math.round(300 + Math.sin(si * Math.PI / 30)	* length);
			int sx2 = (int) Math.round(300 + Math.cos(si * Math.PI / 30	+ Math.PI) * 20);
			int sy2 = (int) Math.round(300 + Math.sin(si * Math.PI / 30	+ Math.PI) * 20);
			g2.drawLine(sx, sy, sx2, sy2);
		}
	}
}
